package zl.io;
import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XMLDemo
{
	public static void main(String[] args)
	{
		DocumentBuilderFactory builderFactory;
		DocumentBuilder builder;
		Document doc;
		Element root;
		File file = new File("Test.xml");
		try
		{
			//实例化一个文档构建工厂对象
			builderFactory = DocumentBuilderFactory.newInstance();
			//根据工厂对象构建一个文档构建对象
			builder = builderFactory.newDocumentBuilder();
			//根据文档构建对象的parse方法加载一个xml文件
			doc = builder.parse(file);
			//读取xml文档的根节点
			root = doc.getDocumentElement();
			//获得根节点的标签名
			System.out.println("根节点名称：" + root.getTagName());
			//获得根节点下标签名为“describe”的子标签集合
			NodeList des = root.getElementsByTagName("describe");
			//取得集合的第一个节点的文本内容
			System.out.println("学校：" + ((Element)des.item(0)).getTextContent());
			for(int i = 0; i < root.getElementsByTagName("Class").getLength(); i++)
			{
				//根据Class节点的Name属性判断是否是E73班
				if(!((Element)root.getElementsByTagName("Class").item(i)).getAttribute("Name").equals("E73"))
				{
					continue;
				}
				//得到Class节点下的Student节点的集合
				NodeList students = ((Element)root.getElementsByTagName("Class").item(i)).getChildNodes();
				//循环遍历E73班Student节点
				for(int j = 0; j < students.getLength(); j++)
				{
					if(students.item(j) instanceof Element)
					{
						//根据属性名取得属性值
						System.out.println("姓名：" + ((Element)students.item(j)).getAttribute("Name") + 
							"\t年龄：" + ((Element)students.item(j)).getAttribute("Age") + "\t性别：" + 
							((Element)students.item(j)).getAttribute("Sex"));
					}
				}
			}
			//删除describe节点
			//((Element)des.item(0)).getParentNode().removeChild(((Element)des.item(0)));
			//修改describe节点
			//((Element)des.item(0)).setTextContent("修改了");
			//给describe添加属性
			//((Element)des.item(0)).setAttribute("Address","深圳南山");

			//实例化一个转换工厂对像
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			//根据工厂实例化一个转换对像
			Transformer transformer = transformerFactory.newTransformer();
			//根据Document对像实例化一个DOMSource对象
			DOMSource domSource = new DOMSource(doc);
			//根据指定文件实例化一个StreamResult对象
			StreamResult streamResult = new StreamResult(file);
			//将Document对象内容写入到指定文件
			transformer.transform(domSource,streamResult);
		}
		catch (ParserConfigurationException e)
		{
			e.printStackTrace();
		}
		catch (SAXException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		catch (TransformerException e)
		{
			e.printStackTrace();
		}
	}
}